package com.sky.controller.admin;

import com.sky.dataTransmit.SetmealDTO;
import com.sky.dataTransmit.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.view.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    @PostMapping
    public Result insert(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐");
        setmealService.insert(setmealDTO);
        return Result.success("新增套餐",null);
    }

    @GetMapping("/page")
    public Result<PageResult> getList(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐");
        PageResult pageResult= setmealService.getList(setmealPageQueryDTO);
        return Result.success("分页查询套餐",pageResult);
    }

    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除套餐");
        setmealService.delete(ids);
        return Result.success("删除套餐",null);
    }

    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id查询套餐");
        SetmealVO setmealVO=setmealService.getById(id);
        return Result.success("根据id查询套餐",setmealVO);
    }

    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐");
        setmealService.update(setmealDTO);
        return Result.success("修改套餐",null);
    }

    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("套餐售卖状态修改");
        setmealService.updateStatus(status,id);
        return Result.success("套餐售卖状态修改",null);
    }
}
